package com.yedou.controller;

import cn.hutool.core.bean.BeanUtil;
import com.yedou.common.Result;
import com.yedou.entity.dto.PageDTO;
import com.yedou.entity.dto.PeopleDto;
import com.yedou.entity.dto.PeopleFormDto;
import com.yedou.entity.dto.PeopleQuery;
import com.yedou.entity.po.People;
import com.yedou.entity.vo.PeopleVO;
import com.yedou.service.IPeopleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/people")
public class PeopleController {


    @Autowired
    private IPeopleService peopleService;

    @ApiOperation("新增用户接口")
    @PostMapping
    public void savePeople(@RequestBody PeopleFormDto peopleDto){
        People people = BeanUtil.copyProperties(peopleDto, People.class);
        peopleService.save(people);
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("/{id}")
    public void deletePeopleById(@PathVariable("id") Long id){
        peopleService.removeById(id);
    }

    @ApiOperation("根据id查询用户接口")
    @GetMapping("/{id}")
    public Result<PeopleVO> queryPeopleById(@PathVariable Long id){
//        People people = peopleService.getById(id);
//        PeopleVO peopleVO = BeanUtil.copyProperties(people, PeopleVO.class);
//        return Result.ok(peopleVO);
        PeopleVO peopleVO  = peopleService.queryPeopleById(id);
        return Result.ok(peopleVO);
    }

    @ApiOperation("根据id批量查询用户接口")
    @GetMapping
    public Result<List<PeopleVO>> queryPeopleById(@RequestParam("ids") List<Long> ids){
        List<People> people = peopleService.listByIds(ids);
        List<PeopleVO> peopleVOS = BeanUtil.copyToList(people, PeopleVO.class);
        return Result.ok(peopleVOS);
    }
    @ApiOperation("扣减用户余额接口")
    @DeleteMapping("/{id}/deduction/{money}")
    public void deductMoneyById(@ApiParam("用户id") @PathVariable("id") Long id,
                                @ApiParam("扣减的金额") @PathVariable("money") Integer money){
        peopleService.deductBalance(id, money);
    }

    @ApiOperation("根据复杂条件批量查询用户接口")
    @GetMapping("list")
    public Result<List<PeopleVO>> queryPeoples(PeopleQuery peopleQuery){
        List<People> peopleList = peopleService.queryPeoples(peopleQuery.getName(),peopleQuery.getStatus(),
                peopleQuery.getMinBalance(), peopleQuery.getMaxBalance());
        List<PeopleVO> peopleVOS = BeanUtil.copyToList(peopleList, PeopleVO.class);
        return Result.ok(peopleVOS);
    }


    @ApiOperation("根据条件分页查询用户接口")
    @GetMapping("/page")
    public PageDTO<PeopleVO> queryUsersByPage(PeopleQuery peopleQuery){

        return peopleService.queryUsersByPage(peopleQuery);
    }

}
